package com.bl.student.service.biz.teacher.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.*;
import com.bl.student.dao.redis.model.TeacherInfo;
import com.bl.student.dao.repository.*;
import com.bl.student.service.biz.hx.HxAuthService;
import com.bl.student.service.biz.teacher.TeacherService;
import com.bl.student.service.biz.user.UserService;
import com.bl.student.service.biz.vip.VipService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import com.bl.student.service.vo.order.OrderListVo;
import com.bl.student.service.vo.saliva.SalivaHistoryListVo;
import com.bl.student.service.vo.saliva.SalivaHistoryVo;
import com.bl.student.service.vo.teacher.*;
import com.bl.student.tp.hx.user.*;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yujingyi on 2018/2/25.
 */
@Service
@Log4j2
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    UserService userService;

    @Autowired
    UserRepository userRepository;

    @Autowired
    VipRepository vipRepository;

    @Autowired
    VipService vipService;

    @Autowired
    TagRepository tagRepository;

    @Autowired
    SalivaHistoryRepository  salivaHistoryRepository;

    @Autowired
    ApplyHistoryRepository applyHistoryRepository;

    @Autowired
    TeacherStudentRepository teacherStudentRepository;

    @Autowired
    UserTagRepository userTagRepository;

    @Autowired
    TeacherTagRepository teacherTagRepository;

    @Autowired
    HxUserApi hxUserApi;

    @Autowired
    HxAuthService hxAuthService;

    @Override
    public TeacherEntity getTeacherByMobile(String account) {
        List<TeacherEntity> teacherEntities = teacherRepository.findByMobile(account);
        return ListUtils.getFirst(teacherEntities);
    }

    @Override
    public TeacherEntity getTeacherByHxName(String hxName) {
        List<TeacherEntity> teacherEntities = teacherRepository.findByMobile(hxName);
        return ListUtils.getFirst(teacherEntities);
    }

    @Override
    public TeacherSimpleInfoVo getTeacherInfo(TeacherInfo teacherInfo) {
        TeacherEntity teacherEntity = teacherRepository.findOne(teacherInfo.getId());
        TeacherSimpleInfoVo teacherSimpleInfoVo = convertTeacherSimpleInfo(teacherEntity);
        return convertTeacherSimpleInfo(teacherEntity, teacherSimpleInfoVo);
    }

    private TeacherSimpleInfoVo convertTeacherSimpleInfo(TeacherEntity teacherEntity) {
        TeacherSimpleInfoVo teacherSimpleInfoVo = new TeacherSimpleInfoVo();
        BeanUtils.copyProperties(teacherEntity, teacherSimpleInfoVo);
        return teacherSimpleInfoVo;
    }

    private TeacherSimpleInfoVo convertTeacherSimpleInfo(TeacherEntity teacherEntity, TeacherSimpleInfoVo teacherSimpleInfoVo) {
        BeanUtils.copyProperties(teacherEntity, teacherSimpleInfoVo);
        return teacherSimpleInfoVo;
    }



    @Override
    public TeacherListVo getUserListMulti(String id, String mobile, String nickname, String status, Pageable pageable) {
        Page <TeacherEntity> teacherEntities;
        TeacherListVo teacherListVo = new TeacherListVo();

        if(id == null && mobile == null && nickname == null && status == null) {
            teacherEntities = teacherRepository.findAll(pageable );
        } else {
            teacherEntities = teacherRepository.findMultList(id, mobile, nickname, status, pageable );
        }
        teacherListVo.setTotalCount(teacherEntities.getTotalElements());
        teacherListVo.setList(teacherEntities.getContent());

        return teacherListVo;
    }

    @Override
    public void del(DelBody id) {
        throw new CommonServiceException(ApiResponseCode.PERMISSION_DENIED.getCode(), "对不起，我们暂不支持老师删除功能");
//        teacherRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        throw new CommonServiceException(ApiResponseCode.PERMISSION_DENIED.getCode(), "对不起，我们暂不支持老师删除功能");
//
//        for(String id : idList.getList()) {
//            teacherRepository.delete(id);
//        }
    }

    @Transactional
    @Override
    public void modify(TeacherModVo teacherModVo) {
        TeacherEntity teacherEntity = teacherRepository.findOne(teacherModVo.getId());
        if (teacherEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST);
        }

        teacherEntity.setName(teacherModVo.getName());
        teacherEntity.setUpdateTime(new Date());
        teacherEntity.setName(teacherModVo.getName());
        teacherEntity.setAvatar(teacherModVo.getAvatar());
        teacherEntity.setPassword(teacherModVo.getPassword());
        teacherEntity.setStatus(teacherModVo.getStatus());
        teacherEntity.setPoint(teacherModVo.getPoint());

        teacherRepository.save(teacherEntity);
    }

    @Override
    public void delApply(DelBody id) {

        ApplyHistoryEntity applyHistoryEntity = applyHistoryRepository.findOne(id.getId());

        List<TeacherStudentEntity> teacherStudentEntity = teacherStudentRepository.findByStudentId(applyHistoryEntity.getUserId());

        if (teacherStudentEntity.size() > 0) {
            teacherStudentRepository.delete(teacherStudentEntity.get(0).getId());
        }

        applyHistoryRepository.delete(id.getId());
    }

    @Override
    public TeacherEntity getTeacherByInviteCode(String inviteCode) {
        List<TeacherEntity> userEntities = teacherRepository.findByInviteCode(inviteCode);
        return ListUtils.getFirst(userEntities);
    }


}
